from nd2reader import ND2Reader
import numpy as np
import imageio
import matplotlib.pyplot as plt
from PIL import Image
from skimage import io
%matplotlib inline
tag=0
# define crop range
range1=[75, 215]
range2=[295, 435]
data_path = 'H:/DH_localization/Experiments_Phase2_afterSPIE/NW_astig_spetrum_data/September2019_CS_3DsSMLM'
im1 = ND2Reader(data_path + '/3DMC_Scan (1).nd2')
im2 = ND2Reader(data_path + '/3DMC_Scan (2).nd2')
im3 = ND2Reader(data_path + '/3DMC_Scan (3).nd2')
im4 = ND2Reader(data_path + '/3DMC_Scan (4).nd2')
im5 = ND2Reader(data_path + '/3DMC_Scan (5).nd2')
im6 = ND2Reader(data_path + '/3DMC_Scan (6).nd2')
im7 = ND2Reader(data_path + '/3DMC_Scan (7).nd2')
im8 = ND2Reader(data_path + '/3DMC_Scan (8).nd2')
im9 = ND2Reader(data_path + '/3DMC_Scan (9).nd2')
im10 = ND2Reader(data_path + '/3DMC_Scan (10).nd2')
im11 = ND2Reader(data_path + '/3DMC_Scan (11).nd2')
im12 = ND2Reader(data_path + '/3DMC_Scan (12).nd2')
im13 = ND2Reader(data_path + '/3DMC_Scan_Lowilumination (1).nd2')
im14 = ND2Reader(data_path + '/3DMC_Scan_Lowilumination (2).nd2')
im15 = ND2Reader(data_path + '/3DMC_Scan_Lowilumination (3).nd2')
im16 = ND2Reader(data_path + '/3DMC_Scan_Lowilumination (4).nd2')
im17 = ND2Reader(data_path + '/3DMC_Scan_Lowilumination (5).nd2')
im18 = ND2Reader(data_path + '/Cali_3D_Crimson (1).nd2')
im19 = ND2Reader(data_path + '/Cali_3D_Crimson (2).nd2')
im20 = ND2Reader(data_path + '/Cali_3D_Crimson (3).nd2')
im21 = ND2Reader(data_path + '/Cali_3D_Crimson (4).nd2')
im22 = ND2Reader(data_path + '/Cali_3D_Crimson (5).nd2')
im23 = ND2Reader(data_path + '/Cali_3D_Crimson (6).nd2')
im24 = ND2Reader(data_path + '/Cali_3D_Crimson (7).nd2')
im25 = ND2Reader(data_path + '/Cali_3D_Darkred (1).nd2')
im26 = ND2Reader(data_path + '/Cali_3D_Darkred (2).nd2')
im27 = ND2Reader(data_path + '/Cali_3D_Darkred (3).nd2')
im28 = ND2Reader(data_path + '/Cali_3D_Darkred (4).nd2')
im29 = ND2Reader(data_path + '/Cali_3D_Darkred (5).nd2')
im30 = ND2Reader(data_path + '/Cali_3D_Darkred (6).nd2')
im31 = ND2Reader(data_path + '/Cali_3D_Darkred (7).nd2')
im32 = ND2Reader(data_path + '/Cali_FieldDistortion_wCL.nd2')
im33 = ND2Reader(data_path + '/Cali_FieldDistortion_woCL.nd2')
im34 = ND2Reader(data_path + '/Cali_Spectral_wCL.nd2')
MCexpt. Aug. 2019
Spectral dispersion: ~ 3.1 nm/pixelPixel size: 181 nm. (a camera pixel size of 16 um with the total magnification: 88.5 (100X OBJ, 1.5X tubelens, 0.59X relayoptics)) All 3D data sets were scanned with stepsize of 25 nm.10 um axial range for Cali_3D 20 um axial range 3DMC_Scan.Exposure time: 30 ms - 200ms.
fig = plt.figure(figsize=[15,8])
plt.subplot(211)
plt.imshow(im1[800])
plt.title('with CL')
print('3DMC_Scan')
for ind in np.arange(12):
exec('im = im'+str(ind+1))
fig = plt.figure(figsize=[15,8])
plt.subplot(231); plt.imshow(im[80].T); plt.axis('off')
plt.subplot(232); plt.imshow(im[180].T); plt.axis('off')
plt.subplot(233); plt.imshow(im[280].T); plt.axis('off')
print('3DMC_Scan_Lowilumination')
for ind in np.arange(5):
exec('im = im'+str(ind+13))
fig = plt.figure(figsize=[15,8])
plt.subplot(231); plt.imshow(im[80].T); plt.axis('off')
plt.subplot(232); plt.imshow(im[180].T); plt.axis('off')
plt.subplot(233); plt.imshow(im[280].T); plt.axis('off')
print('Cali_3D_Crimson')
for ind in np.arange(7):
exec('im = im'+str(ind+18))
fig = plt.figure(figsize=[15,8])
plt.subplot(231); plt.imshow(im[80].T); plt.axis('off')
plt.subplot(232); plt.imshow(im[180].T); plt.axis('off')
plt.subplot(233); plt.imshow(im[280].T); plt.axis('off')
print('Cali_3D_Darkred')
for ind in np.arange(7):
exec('im = im'+str(ind+25))
fig = plt.figure(figsize=[15,8])
plt.subplot(231); plt.imshow(im[80].T); plt.axis('off')
plt.subplot(232); plt.imshow(im[180].T); plt.axis('off')
plt.subplot(233); plt.imshow(im[280].T); plt.axis('off')
#for ind in np.arange(1):
for ind in np.arange(34):
exec('im = im'+str(ind+1))
print(ind)
plt.figure(figsize=(10,10))
plt.subplot(121)
if ind < 31:
plt.imshow(im[180][range1[0]:range1[1]])
else:
plt.imshow(im[0][range1[0]:range1[1]])
plt.subplot(122)
if ind < 31:
plt.imshow(im[180][range2[0]:range2[1]])
else:
plt.imshow(im[0][range2[0]:range2[1]])
import os import imageio import numpy as np thedir=data_path fs = [name for name in os.listdir(thedir) if os.path.isfile(os.path.join(thedir, name))] data_path_tifs = 'G:/DH_localization/Experiments_Phase2_afterSPIE/NW_astig_spetrum_data/September2019_CS_3DsSMLM_tiffs' for f in fs[0:-1]: im = ND2Reader(data_path + '/' + f) imageio.mimwrite(data_path_tifs + '/' + f[0:-4]+'.tif',np.asarray(im)) imar = np.asarray(im) ims1=imar[:,range1[0]:range1[1],:] ims0=imar[:,range2[0]:range2[1],:] imageio.mimwrite(data_path_tifs + '/' + f[0:-4]+'_dif0CL.tif',ims0) imageio.mimwrite(data_path_tifs + '/' + f[0:-4]+'_dif1.tif',ims1)
# Now crop field of views for all dataset.
import pandas as pd
peaksf=data_path+'/Sep2019_bead_coordinates.xlsx'
df = pd.read_excel(peaksf) #for an earlier version of Excel, you may need to use the file extension of 'xls'
df.head()
def cropPSF3D(des_dims, ori_dims, widths, ori_coords, ori_stack):
ori_sta_t = list()
ori_sta = list()
ori_end = list()
des_sta = list()
des_end = list()
for ind, x in enumerate(zip(des_dims, ori_dims, widths, ori_coords)):
d_dim = x[0]
o_dim = x[1]
w = x[2]
o_coord = x[3]
ost = o_coord - w # original matrix, starting index, temp
os = max(0, ost) # original matrix, starting index.
oe = min(o_dim-1, o_coord + w) # original matrix, ending index
ds = max(0, w - (o_coord - os)) # destimation matrix, starting index
de = min(d_dim, w + (oe - o_coord)) # destination matrix, ending index
ori_sta_t.append(ost)
ori_sta.append(os)
ori_end.append(oe)
des_sta.append(ds)
des_end.append(de)
des_stack = np.zeros((des_dims))
des_stack[des_sta[0]:des_end[0], des_sta[1]:des_end[1], des_sta[2]:des_end[2]] = ori_stack[ori_sta[0]:ori_end[0], ori_sta[1]:ori_end[1], ori_sta[2]:ori_end[2]]
return des_stack
print(des_stack.shape)
print('ori_dims:' + str(ori_dims))
print('ori_coords:' + str(ori_coords))
print('widths:' + str(widths))
print('ori_sta_t' + str(ori_sta_t))
print('ori_sta:' + str(ori_sta))
print('ori_end:' + str(ori_end))
print('')
print('des_dims:' + str(des_dims))
print('des_sta:' + str(des_sta))
print('des_end:' + str(des_end))
f = df['file name'][1]
im = np.asarray(ND2Reader(data_path + '/' + f))
l = cropPSF3D(des_dims = np.asarray([201,101,101]),
ori_dims = np.asarray(im.shape),
widths = np.asarray([100,50,50]),
ori_coords = np.asarray([188, 354, 78]),
ori_stack = im)
plt.imshow(l[100,:,:])
df['width xy'][0]
w_xy = np.int(df['width xy'][0])
w_z = np.int(df['width z'][0])
psf_stack = np.zeros([w_z*2+1, w_xy*2+1, w_xy*2+1])
fig = plt.figure(figsize=[15,60])
for ind in np.arange(1,31):
print(ind)
m = df.loc[ind][0][0:-4]+' bead' +' '+str(df.loc[ind][1])
f = df['file name'][ind]
im = np.asarray(ND2Reader(data_path + '/' + f))
psf_stack_stamp1 = cropPSF3D(des_dims = np.asarray([201,101,101]),
ori_dims = np.asarray(im.shape),
widths = np.asarray([100,50,50]),
ori_coords = np.asarray([np.int(df['z'][ind]), np.int(df['0th order y'][ind]), np.int(df['0th order x'][ind])]),
ori_stack = im)
plt.subplot(15,4,ind*2-1)
plt.imshow(psf_stack_stamp1[56,:,:])
plt.title(str(df['file name'][ind]) + ' bead-' + str(df['bead index'][ind]))
psf_stack_stamp2 = cropPSF3D(des_dims = np.asarray([201,101,101]),
ori_dims = np.asarray(im.shape),
widths = np.asarray([100,50,50]),
ori_coords = np.asarray([np.int(df['z'][ind]), np.int(df['1st order y'][ind]), np.int(df['1st order x'][ind])]),
ori_stack = im)
plt.subplot(15,4,ind*2)
plt.imshow(psf_stack_stamp2[56,:,:])
plt.title(ind)
#plt.title(str(ind))
print(m)
fig = plt.figure(figsize=[15,60])
for ind in np.arange(1,31):
print(ind)
m = df.loc[ind][0][0:-4]+' bead' +' '+str(df.loc[ind][1])
f = df['file name'][ind]
im = np.asarray(ND2Reader(data_path + '/' + f))
psf_stack_stamp1 = cropPSF3D(des_dims = np.asarray([201,101,101]),
ori_dims = np.asarray(im.shape),
widths = np.asarray([100,50,50]),
ori_coords = np.asarray([np.int(df['z'][ind]), np.int(df['0th order y'][ind]), np.int(df['0th order x'][ind])]),
ori_stack = im)
plt.subplot(15,4,ind*2-1)
plt.imshow(psf_stack_stamp1[100,:,:])
plt.title(str(df['file name'][ind]) + ' bead-' + str(df['bead index'][ind]))
psf_stack_stamp2 = cropPSF3D(des_dims = np.asarray([201,101,101]),
ori_dims = np.asarray(im.shape),
widths = np.asarray([100,50,50]),
ori_coords = np.asarray([np.int(df['z'][ind]), np.int(df['1st order y'][ind]), np.int(df['1st order x'][ind])]),
ori_stack = im)
plt.subplot(15,4,ind*2)
plt.imshow(psf_stack_stamp2[100,:,:])
plt.title(ind)
#plt.title(str(ind))
print(m)
fig = plt.figure(figsize=[15,5])
ind = 16
m = df.loc[ind][0][0:-4]+' bead' +' '+str(df.loc[ind][1])
f = df['file name'][ind]
im = np.asarray(ND2Reader(data_path + '/' + f))
DR0 = cropPSF3D(des_dims = np.asarray([201,101,101]),
ori_dims = np.asarray(im.shape),
widths = np.asarray([100,50,50]),
ori_coords = np.asarray([np.int(df['z'][ind]), np.int(df['0th order y'][ind]), np.int(df['0th order x'][ind])]),
ori_stack = im)
plt.subplot(1,2,1)
plt.imshow(DR0[100,:,:])
plt.title(str(df['file name'][ind]) + ' bead-' + str(df['bead index'][ind]))
DR1 = cropPSF3D(des_dims = np.asarray([201,101,101]),
ori_dims = np.asarray(im.shape),
widths = np.asarray([100,50,50]),
ori_coords = np.asarray([np.int(df['z'][ind]), np.int(df['1st order y'][ind]), np.int(df['1st order x'][ind])]),
ori_stack = im)
plt.subplot(1,2,2)
plt.imshow(DR1[100,:,:])
plt.title(ind)
fig = plt.figure(figsize=[15,5])
ind = 25
m = df.loc[ind][0][0:-4]+' bead' +' '+str(df.loc[ind][1])
f = df['file name'][ind]
im = np.asarray(ND2Reader(data_path + '/' + f))
CS0 = cropPSF3D(des_dims = np.asarray([201,101,101]),
ori_dims = np.asarray(im.shape),
widths = np.asarray([100,50,50]),
ori_coords = np.asarray([np.int(df['z'][ind]), np.int(df['0th order y'][ind]), np.int(df['0th order x'][ind])]),
ori_stack = im)
plt.subplot(1,2,1)
plt.imshow(DR0[100,:,:])
plt.title(str(df['file name'][ind]) + ' bead-' + str(df['bead index'][ind]))
CS1 = cropPSF3D(des_dims = np.asarray([201,101,101]),
ori_dims = np.asarray(im.shape),
widths = np.asarray([100,50,50]),
ori_coords = np.asarray([np.int(df['z'][ind]), np.int(df['1st order y'][ind]), np.int(df['1st order x'][ind])]),
ori_stack = im)
plt.subplot(1,2,2)
plt.imshow(DR1[100,:,:])
plt.title(ind)
import imageio
data_path_tifs = 'H:/DH_localization/Experiments_Phase2_afterSPIE/NW_astig_spetrum_data/September2019_CS_3DsSMLM_tiffs'
imageio.mimwrite(data_path_tifs + '/PSF_Darkred_Order0.tif',np.asarray(DR0,'int'))
imageio.mimwrite(data_path_tifs + '/PSF_Darkred_Order1.tif',np.asarray(DR1,'int'))
imageio.mimwrite(data_path_tifs + '/PSF_Crimson_Order0.tif',np.asarray(CS0,'int'))
imageio.mimwrite(data_path_tifs + '/PSF_Crimson_Order1.tif',np.asarray(CS1,'int'))
df
np.asarray([df['z'][ind], df['0th order y'][ind], df['0th order x'][ind]])
path="H:\\DH_localization\\Experiments_Phase2_afterSPIE\\NW_astig_spetrum_data\\September2019_CS_3DsSMLM_tiffs"
slices_file = 'H:\\DH_localization\\Experiments_Phase2_afterSPIE\\NW_astig_spetrum_data\\September2019_CS_3DsSMLM\\Sep2019_recovery_slices.xlsx';
sf = pd.read_excel(slices_file)
sf.head()
outputpath="C:\\Users\\china\\Desktop\\Research\\PyPRIS\\test_dataset_6";
for ind in np.arange(0,4):
data = path + '\\' + sf['fname'][ind]
print(data1)
for imsuffix in ['_dif1.tif', '_dif0CL.tif']:
im = np.array(io.imread(data[0:-4]+ imsuffix))
plt.figure(figsize=[15, 10])
for frameN in [1, 2, 3]:
n = outputpath+'\\view'+str(sf['viewN'][ind]) +'_frame'+str(sf['frame'+str(frameN)][ind]) + imsuffix;
m = im[int(sf['frame'+str(frameN)][ind]),:,:]
plt.subplot(1,3,frameN)
plt.imshow(m);imageio.imwrite(n,m);
plt.title('view'+str(ind)+' frame' + str(sf['frame'+str(frameN)][ind]))
print(n)
path="H:\\DH_localization\\Experiments_Phase2_afterSPIE\\NW_astig_spetrum_data\\September2019_CS_3DsSMLM_tiffs"
outputpath="C:\\Users\\china\\Desktop\\Research\\PyPRIS\\test_dataset_6";
n1 = outputpath + "\\Cali_3D_Crimson (3) single slice"
n2 = outputpath + "\\Cali_3D_Darkred (5) single slice"
data1 = path + '\\' + "Cali_3D_Crimson (3).tif"
data2 = path + '\\' + "Cali_3D_Darkred (5).tif"
im1 = np.array(io.imread(data1))
im2 = np.array(io.imread(data2))
m1 = im1[192,:,:]
m2 = im2[188,:,:]
plt.figure(figsize=[10,10])
plt.subplot(221)
plt.imshow(im1[192, range1[0]:range1[1],:]);plt.title('dif1')
plt.subplot(222)
plt.imshow(im2[188, range1[0]:range1[1],:])
plt.subplot(223)
plt.imshow(im1[192, range2[0]:range2[1],:]); plt.title('dif0CL')
plt.subplot(224)
plt.imshow(im2[188, range2[0]:range2[1],:])
imageio.imwrite(n1+"_dif1.tif",im1[192, range1[0]:range1[1],:]);
imageio.imwrite(n1+"_dif0CL.tif",im1[192, range2[0]:range2[1],:]);
imageio.imwrite(n2+"_dif1.tif",im2[188, range1[0]:range1[1],:]);
imageio.imwrite(n2+"_dif0CL.tif",im2[188, range2[0]:range2[1],:]);
# for ind in np.arange(34):
# exec('im = im'+str(ind+1))
# print(ind)
# plt.figure(figsize=(10,10))
# plt.subplot(121)
# if ind < 31:
# plt.imshow(im[180][range1[0]:range1[1]])
# else:
# plt.imshow(im[0][range1[0]:range1[1]])
# plt.subplot(122)
# if ind < 31:
# plt.imshow(im[180][range2[0]:range2[1]])
# else:
# plt.imshow(im[0][range2[0]:range2[1]])